﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace FormatDictonaryXml
{
    class Program
    {
        static void Main(string[] args)
        {
            String path = @"PMP_ForYouDao.xml";
            String outputPath = @"BindDict.xml";
            String header = "<?xml version=\"1.0\"?>";
            String fCVocaPhraseList = "<FCVocaPhraseList xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">";
            String FCVocaPhraseListEnd = "</FCVocaPhraseList>";
            String pharses =  "<Phrases>";
            String pharsesEnd = "</Phrases>";
            String phaseFormat = @"
             <Phrase>
      <Eng>{0}</Eng>
      <Phonetic> </Phonetic>
      <Defi>{1}</Defi>
      <Date>2014-07-11 10:17:27</Date>
      <Note />
    </Phrase>";


            XElement xdoc = XElement.Load(path);
            try
            {
                XElement xdoc2 = XElement.Load(outputPath).Element("Phrases");
                int elements = xdoc2.Elements().Count();
                var ret = xdoc2.Elements().Skip(143).ToList();


            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex);
            }
            StringBuilder builder = new StringBuilder();
            builder.AppendLine(header);
            builder.AppendLine(fCVocaPhraseList);
            builder.AppendLine(pharses);

            foreach (XElement element in xdoc.Elements())
            { 
                String word = element.Element("word").Value.Trim();
                if (String.IsNullOrEmpty(word))
                    continue;
                String trans = element.Element("trans").Value.Trim();
                trans = trans.Replace("<![CDATA[","");
                trans = trans.Replace("]]>", "");

                String phase = String.Format(phaseFormat,word,trans);
                builder.AppendLine(phase);
            }
            builder.AppendLine(pharsesEnd);
            builder.AppendLine(FCVocaPhraseListEnd);
            File.WriteAllText(outputPath, builder.ToString());
        
        }
    }
}
